package com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class SocketClient1 implements Runnable{
	
	private int number;
	
	public SocketClient1(){}
	
	public SocketClient1(int number){
		this.number = number;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try{
			for(int i = 0; i < 5; i++){
				SocketClient1 client = new SocketClient1(i);
				Thread thread = new Thread(client);
				thread.start();
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		try{
			Socket socket = new Socket("localhost",2013);
			BufferedWriter bufWriter = new BufferedWriter(new PrintWriter(socket.getOutputStream()));
			BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
			int times = 3;
			while(times > 0){
				bufWriter.write("I'm client_" +number+ " and send for " + times + "\n");
				bufWriter.flush();
				times--;
				String respMsg = reader.readLine();
				System.out.println("I'm client_" +number+ " and receive " + respMsg);
			}
			bufWriter.write("Bye");
			bufWriter.flush();
			bufWriter.close();
			reader.close();
			socket.close();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

}
